//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.chris.serve;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chris.entity.root;
import com.chris.impl.rootImpl;
import com.chris.utils.DbConnection;
import com.chris.utils.JsonProcess;
import com.chris.utils.LogUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet({"/loginRoot"})
public class loginRoot extends HttpServlet {
    private final rootImpl rootDao = new rootImpl();

    public loginRoot() {
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject read = JsonProcess.read(req.getInputStream());
        PrintWriter writer = resp.getWriter();
        try {
            String account = (String)read.get("account");
            String password = (String)read.get("password");
            root userByAccount = this.rootDao.getUserByAccount(account);
            if (userByAccount == null) {
                writer.print(JSON.toJSONString(JsonProcess.respError("用户不存在")));
            } else {
                String pwd = userByAccount.getPassword();
                if (!pwd.equals(password)) {
                    writer.print(JsonProcess.respError("密码错误"));
                } else {
                    JSONObject jsb = new JSONObject();
                    jsb.put("account", userByAccount.getAccount());
                    jsb.put("name", userByAccount.getName());
                    jsb.put("age", userByAccount.getAge());
                    jsb.put("sex", userByAccount.getSex());
                    String s = Base64.getEncoder().encodeToString(userByAccount.getAccount().getBytes(StandardCharsets.UTF_8));
                    jsb.put("authorization", s);
                    writer.print(JsonProcess.respSuccess("登录成功", jsb));
                }
            }
        } catch (Exception var11) {
            writer.print(JsonProcess.respError("参数错误"));
            LogUtil.log("参数错误",var11);
        }
        DbConnection.endServe();
    }

    public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.addHeader("Access-Control-Allow-Headers", "*");
    }
}
